/*!
 * \file frct_hash_del.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frlist.h"
#include "frsutl.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frct_hash.h"
#include "frct_mng.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

void __hash_node_free(Frlist_node *p_st_node);

extern Frhash_header        *g_p_st_hash_ct;

static void __case1(void)
{
    Frlist_node *p_st_node = NULL;
    int i_ret = 0;

    g_p_st_hash_ct=NULL;

    i_ret = frct_hash_del(p_st_node);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);

    return;
}


static void __case2(void)
{
    Frlist_node *p_st_node = NULL;
    Conntrack *p_st_ct = NULL;
    char *p_key = NULL;
    int i_key_len = 128;
    u_short ip_proto = 6;
    u_int ui_src = iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_port = 22;
    u_int ui_dest = iptools_parse_ipaddr((char *)"192.168.0.1");
    u_short us_dport = 80;
    unsigned int ui_table_size = 65535;
    int i_ret = 0;

    g_p_st_hash_ct=NULL;
    frct_hash_init(ui_table_size);

    p_key = (char *)frmalloc(i_key_len);

    i_key_len = frct_hash_key(ip_proto, ui_src, us_port, ui_dest, us_dport, p_key, i_key_len);

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    p_st_ct->src_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_st_ct->src_port = 80;
    p_st_ct->dst_ip = iptools_parse_ipaddr((char *)"192.168.0.1");
    p_st_ct->dst_port = 22;
    p_st_ct->ip_proto = 6;

    p_st_node = frct_hash_add(p_st_ct, p_key, i_key_len);

    p_st_node = frct_hash_get(ip_proto, ui_src, us_port, ui_dest, us_dport);

    i_ret = frct_hash_del(p_st_node);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    p_st_node = frct_hash_get(ip_proto, ui_src, us_port, ui_dest, us_dport);

    CU_ASSERT_PTR_NULL(p_st_node);

    frfree(p_key);
    frfree(p_st_ct);
    frhash2_free2(g_p_st_hash_ct,__hash_node_free);

    return;
}


int add_suite_frct_hash_del(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frct_hash frct_hash_del()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
//    CU_add_test(pSuite, "case3", __case3);
//    CU_add_test(pSuite, "case4", __case4);

    return FR_SUCC;
}
